package com.boarsoft.boar.gateway.weixin.common.http;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.boar.gateway.wechat.common.WechatConfig;

/**
 * 微信http处理
 * @author liucp
 *
 */
public class WXHttpProcesser {

	private static Logger log = LoggerFactory.getLogger(WXHttpProcesser.class);
	
	public static boolean isProxy;
	public static String proxyHost;
	public static int proxyPort;
	public static int timeOut;
	private static int httpsPort=443;
	
	static{
		try{
			isProxy=Boolean.valueOf(WechatConfig.getProperty("weixin.wxProxy.isProxy"));
			if(isProxy){
				proxyHost=WechatConfig.getProperty("weixin.wxProxy.proxyHost");
				proxyPort=Integer.parseInt(WechatConfig.getProperty("weixin.wxProxy.proxyPort"));
			}
			timeOut=Integer.parseInt(WechatConfig.getProperty("weixin.wxProxy.wxTimeOut"));
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public static String sendRequest(String targetUrl,String reqMethod,String sendMsg){
		if(reqMethod.equalsIgnoreCase("GET")){
			return sendGetRequest(targetUrl,sendMsg);
		}else if(reqMethod.equalsIgnoreCase("POST")){
			return sendPostRequest(targetUrl,sendMsg);
		}else{
			return "";
		}
	}
	/**
	 * 
	 * @param targetUrl
	 * @param sendMsg
	 * @return
	 * @throws TranFailException
	 */
	public static String sendPostRequest(String targetUrl,String sendMsg){
		HttpClient httpClient = new HttpClient();
		
		if(isProxy){	
			httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort); 
		}
		
		Protocol httpsProtocol = new Protocol("https", new EasySSLProtocolSocketFactory(), httpsPort);
		Protocol.registerProtocol("https", httpsProtocol);
		
		PostMethod postMethod = null;
		
		postMethod  = new PostMethod(targetUrl);
		postMethod.addRequestHeader("Content-Type", "text/xml; charset=UTF-8");
		
		httpClient.setTimeout(timeOut);
		postMethod.setRequestBody(sendMsg);
		try{
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
				log.error("与微信后台通讯失败");
				return "EBWX_COMM";
			}
			String retMsg=new String(postMethod.getResponseBody(),"UTF-8");//微信返回信息
			return retMsg;
		}catch(Exception e){
			log.error("与微信后台通讯异常：", e);
			return "EBWX_COMM";
		} finally{
			postMethod.releaseConnection();	 
		}
	}
	
	public static String sendGetRequest(String targetUrl,String sendMsg){
		HttpClient httpClient = new HttpClient();
		log.info("targetUrl：" + targetUrl);
		log.info("sendMsg：" + sendMsg);
		log.info("proxyHost：" + proxyHost);
		log.info("proxyPort：" + proxyPort);
		if(isProxy){	
			httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort); 
		}
		
		Protocol httpsProtocol = new Protocol("https", new EasySSLProtocolSocketFactory(), httpsPort);
		Protocol.registerProtocol("https", httpsProtocol);
		
		GetMethod GetMethod = null;
		
		GetMethod  = new GetMethod(targetUrl);
		GetMethod.addRequestHeader("Content-Type", "text/xml; charset=UTF-8");
		
		httpClient.setTimeout(timeOut);
		
		try{
			int statusCode = httpClient.executeMethod(GetMethod);
			if (statusCode != HttpStatus.SC_OK) {
				log.error("与微信后台通讯失败");
				return "EBWX_COMM";
			}
			String retMsg=new String(GetMethod.getResponseBody(),"UTF-8");//微信返回信息
			return retMsg;
		}catch(Exception e){
			log.error("与微信后台通讯异常：", e);
			return "EBWX_COMM";
		} finally{
			GetMethod.releaseConnection();	 
		}
	}
}
